﻿using System;

namespace Kangaroo.Deferreds
{
    internal class ConstrainedDeferred<T>:IDeferred<T>
    {
        private readonly IDeferred<T> _deferredToWrap;

        public ConstrainedDeferred(IDeferred<T> deferredToWrap)
        {
            _deferredToWrap = deferredToWrap;
        }

        public DeferredState State
        {
            get { return _deferredToWrap.State; }
        }

        public IDeferred<T> OnDone(params Action<T>[] doneCallbacks)
        {
            return _deferredToWrap.OnDone(doneCallbacks);
        }

        public void Resolve(T result)
        {
            throw new NotSupportedException("A constrained deferred object cannot be resolved.");
        }

        public IDeferred<T> OnFail(params Action<Exception>[] failCallbacks)
        {
            return _deferredToWrap.OnFail(failCallbacks);
        }

        public void Reject(Exception exception)
        {
            throw new NotSupportedException("A constrained deferred object cannot be rejected.");
        }

        public IDeferred<T> Then(Action<T> doneCallback, Action<Exception> failCallback = null)
        {
            return _deferredToWrap.Then(doneCallback, failCallback);
        }

        public IDeferred<TResult> Pipe<TResult>(Func<T, TResult> doneTransform, Func<Exception, Exception> failTransform = null)
        {
            return _deferredToWrap.Pipe(doneTransform, failTransform);
        }

        public IDeferred<TResult> Chain<TResult>(Func<T, IDeferred<TResult>> doneChain, Func<Exception, IDeferred<TResult>> failChain = null)
        {
            return _deferredToWrap.Chain(doneChain, failChain);
        }

        public IDeferred<T> Promise()
        {
            return _deferredToWrap.Promise();
        }
    }
}
